package com.zeng.plugin;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.zeng.plugin.entity.TableEntity;
import com.zeng.plugin.intefaceHandle.impl.*;
import com.zeng.plugin.intefaceHandle.ParseHandle;
import com.zeng.plugin.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Mojo(name = "sayhi")
public class MyMojo extends AbstractMojo {

    @Parameter(property = "sayhi.voModule")
    String voModule;
    @Parameter(property = "sayhi.serviceimplModule")
    String serviceimplModule;

    @Parameter(property = "sayhi.serviceModule")
    String serviceModule;

    @Parameter(property = "sayhi.controllerModule")
    String controllerModule;

    @Parameter(property = "basedir")
    String basedir;

    @Parameter(property = "project.build.directory")
    String directory;

    @Parameter(property = "project.groupId")
    String groupId;

    @Parameter(property = "project.artifactId")
    String artifactId;

    String javaPath = new File("").getAbsolutePath() + "\\src\\main\\java";

    @Parameter(property = "sayhi.prefix", defaultValue = "yl")
    String prefix;

    @Parameter(property = "sayhi.mapperModule")
    String mapperModule;

    @Parameter(property = "sayhi.pojoModule")
    String pojoModule;

    /*
     * mapperPackage
     * entityPackage
     * servicePackage
     * serviceimplPackage
     * voPackage
     * controllerPackage
     * */
    @Parameter(property = "sayhi.packageMaps")
    Map<String, String> packageMaps;

    private String tableInfoXmlName = "\\tables.xml";

    private String tamplateDir;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        this.directory = new File("./").getAbsolutePath();
        this.tamplateDir = new File(directory + "\\template").getAbsolutePath();
        getLog().info(basedir);

        if (StringUtils.isEmpty(packageMaps.get("entityPackage"))) {
            throw new RuntimeException("entityPackage不能为空");
        }
        initWraperPackage(packageMaps);

        String mapperJavaPath = getJavaPath(directory, mapperModule, packageMaps.get("mapperPackage"));
        String voJavaPath = getJavaPath(directory, voModule, packageMaps.get("voPackage"));
        String servicePath = getJavaPath(directory, serviceModule, packageMaps.get("servicePackage"));
        String serviceimplPath = getJavaPath(directory, serviceimplModule, packageMaps.get("serviceimplPackage"));
        String controllerPath = getJavaPath(directory, controllerModule, packageMaps.get("controllerPackage"));
        String wrapperPath = getJavaPath(directory, voModule, packageMaps.get("wrapperPackage"));

        getLog().info("entityPackage --:" + packageMaps.get("entityPackage"));
        getLog().info("directory --:" + directory);
        getLog().info("prefix --:" + prefix);

        String pojoResourceDir = new File(".\\" + pojoModule + "\\src\\main\\resources").getAbsolutePath();
        getLog().info("pojoResourceDir --:" + pojoResourceDir);
        CreatePojo createPojo = new CreatePojo(pojoResourceDir, packageMaps.get("entityPackage"), prefix);
        try {
            FreeMarkUtil freeMarkUtil = new FreeMarkUtil(tamplateDir);
            ParseXmlUtil parseXmlUtil = new ParseXmlUtil();
            parseXmlUtil.parse(new File(tamplateDir + tableInfoXmlName).getAbsolutePath());
            List<TableEntity> tableEntities = parseXmlUtil.getTableEntities();
            createPojo.createPoJo(tableEntities, freeMarkUtil);

            CreateFactory.createMapper(packageMaps.get("mapperPackage"), mapperJavaPath, tableEntities, packageMaps.get("entityPackage"), freeMarkUtil);
            CreateFactory.createByTemplate(packageMaps, voJavaPath, tableEntities, freeMarkUtil, "voTemplate.ftl", new VoParseHandle());
            CreateFactory.createByTemplate(packageMaps, servicePath, tableEntities, freeMarkUtil, "Service.java", new ServiceParseHandle());
            CreateFactory.createByTemplate(packageMaps, serviceimplPath, tableEntities, freeMarkUtil, "ServiceImpl.java", new ServiceimplParseHandle());
            CreateFactory.createByTemplate(packageMaps, controllerPath, tableEntities, freeMarkUtil, "controller.ftl", new ControllerParseHandle());
            CreateFactory.createByTemplate(packageMaps, wrapperPath, tableEntities, freeMarkUtil, "Wrapper.java", new WrapperParseHandle());
        } catch (Exception e) {
            e.printStackTrace();
            getLog().error(e.getMessage());
        }

    }

    public String getJavaPath(String absolutePaht, String module, String packageName) {
        return new File(absolutePaht + "/" + module + "\\src\\main\\java\\" + packageName.replace(".", "/")).getAbsolutePath();
    }

    private void initWraperPackage(Map<String, String> map) {
        String voPackage = map.get("voPackage");
        String[] split = voPackage.split("\\.");
        split[(split.length - 1)] = "wrapperImpl";
        String join = StringUtils.join(split, '.');
        map.put("wrapperPackage", join);
    }

}
